//
//  HistogramBlockView.m
//  Task Views
//
//  Created by Chris Karr on 3/18/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "HistogramBlockView.h"
#import "HistogramView.h"

@implementation HistogramBlockView

@synthesize task;
@synthesize highlighted;
@synthesize lastDown;

- (BOOL) acceptsFirstResponder
{
	return YES;
}

- (id)initWithFrame:(NSRect)frame 
{
    self = [super initWithFrame:frame];
	
    if (self) 
	{
		self.highlighted = [NSNumber numberWithBool:NO];
		self.lastDown = [NSDate date];
    }
	
    return self;
}

- (void)drawRect:(NSRect)rect 
{
	NSUserDefaults * defaults = [NSUserDefaults standardUserDefaults];

	if ([self.highlighted boolValue])
	{
		NSColor * selectColor = [NSColor whiteColor];

		NSData * color = [defaults valueForKey:@"hist_selection_color"];
		if (color != nil)
			selectColor = [NSKeyedUnarchiver unarchiveObjectWithData:color];
		
		[selectColor setFill];
	}
	else
	{
		NSData * color = [defaults valueForKey:@"hist_default_color"];
		
		NSColor * defaultColor = [NSColor redColor];
		if (color != nil)
			defaultColor = [NSKeyedUnarchiver unarchiveObjectWithData:color];

		[defaultColor setFill];
	}
	
	NSBezierPath * path = [NSBezierPath bezierPathWithRect:rect];
	
	[path fill];
}

- (void)mouseDown:(NSEvent *)theEvent
{
	[((HistogramView *) [self superview]) setSelectedItem:self.task];
	
	if ([self.lastDown timeIntervalSinceNow] > -1)
		[((HistogramView *) [self superview]) editTask];

	self.lastDown = [NSDate date];
}

- (void) viewDidMoveToWindow 
{
	tag = [self addTrackingRect:[self bounds] owner:self userData:nil assumeInside:NO];
}

@end
